#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
##############################################################################
#
#
#         ___           ___           ___           ___
#        /  /\         /  /\         /  /\         /  /\
#       /  /::\       /  /:/_       /  /:/_       /  /::\
#      /  /:/\:\     /  /:/ /\     /  /:/ /\     /  /:/\:\
#     /  /:/~/:/    /  /:/ /:/_   /  /:/ /::\   /  /:/~/:/
#    /__/:/ /:/___ /__/:/ /:/ /\ /__/:/ /:/\:\ /__/:/ /:/
#    \  \:\/:::::/ \  \:\/:/ /:/ \  \:\/:/~/:/ \  \:\/:/
#     \  \::/~~~~   \  \::/ /:/   \  \::/ /:/   \  \::/
#      \  \:\        \  \:\/:/     \__\/ /:/     \  \:\
#       \  \:\        \  \::/        /__/:/       \  \:\
#        \__\/         \__\/         \__\/         \__\/
#
#
#
#
#   This file is part of ReSP.
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
#
#   (c) Giovanni Beltrame, Luca Fossati
#       Giovanni.Beltrame@esa.int fossati@elet.polimi.it
#
##############################################################################


import Options
import sys,os

def build(bld):

    # Build and compile the TLM wrapper extension module
    obj = bld.new_task_gen('pypp')

    obj.source ='''
    trap_wrapper.hpp
    '''
    obj.target = 'trapwrapper'
    obj.start_decls = 'trap'
    obj.split = 3

    obj.custom_code = """
OSEmulatorBase = mb.global_ns.class_('OSEmulatorBase')
OSEmulatorBase.include()
for attrName in ['env', 'heapPointer', 'programArgs', 'sysconfmap']:
    OSEmulatorBase.variables(lambda decl: attrName in decl.name).exclude()
tcls = mb.global_ns.classes(lambda decl: decl.name.startswith('ToolsIf'), allow_empty=True)
for i in tcls:
    i.include()
ocls = mb.global_ns.classes(lambda decl: decl.name.startswith('OSEmulator<'), allow_empty=True)
for i in ocls:
    i.include()
    if i.name == 'OSEmulator<unsigned int>':
        i.alias = 'OSEmulator32'
    for j in i.member_functions('initSysCalls'):
        if len(j.arguments) > 2:
            j.exclude()
ocls = mb.global_ns.classes(lambda decl: decl.name.startswith('Profiler<'), allow_empty=True)
for i in ocls:
    i.include()
    if i.name == 'Profiler<unsigned int>':
        i.alias = 'Profiler32'
mb.global_ns.class_('ABIIf<unsigned int>').include()

mb.global_ns.classes(lambda decl: decl.name.startswith('ToolsManager'), allow_empty=True).include()
mb.global_ns.classes(lambda decl: decl.name.startswith('MemoryToolsIf'), allow_empty=True).ignore = False
abiIfDecl = mb.global_ns.classes(lambda decl: 'ABIIf' in decl.name, allow_empty=True)
for i in abiIfDecl:
    i.member_function('getState').call_policies = call_policies.return_value_policy( call_policies.return_addressof )
    i.member_function('getInstructionHistory').call_policies = call_policies.return_value_policy( call_policies.reference_existing_object )


trap_names = mb.namespaces(lambda x: x.name == 'trap')
for cur_namespace in trap_names:
    for i in cur_namespace.free_functions(allow_empty=True):
        i.alias = 'TRAPW_'+i.alias
    for i in cur_namespace.casting_operators(allow_empty=True):
        i.alias = 'TRAPW_'+i.alias
    for i in cur_namespace.classes(allow_empty=True):
        i.alias = 'TRAPW_'+i.alias
    for i in cur_namespace.enumerations(allow_empty=True):
        i.alias = 'TRAPW_'+i.alias
    for i in cur_namespace.variables(allow_empty=True):
        i.alias = 'TRAPW_'+i.alias
    for i in cur_namespace.free_operators(allow_empty=True):
        i.alias = 'TRAPW_'+i.alias
    for i in cur_namespace.typedefs(allow_empty=True):
        i.alias = 'TRAPW_'+i.alias
"""
    obj.uselib = 'SYSTEMC_H TLM TRAP BOOST_PYTHON BOOST_THREAD BOOST_SYSTEM BOOST'
    obj.uselib_local = 'systemc binutils'
    obj.include = 'scwrapper tlmwrapper'
    obj.templates = [
        'trap::OSEmulator< unsigned int>'
        ,'trap::Profiler< unsigned int>'
        ,'trap::ABIIf< unsigned int >'
    ]
